﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using edowork.com.SQL2005;

using System.Web.Resources;
using Microsoft.Reporting.WebForms;

namespace WebInvoicingsys.system.storage.purchasein
{
    public partial class purchasein_recv_print : System.Web.UI.Page
    {
        //传入的参数
        string pcsid;

        private void Return()
        {
            string url = "/system/storage/purchasein/purchasein_recv.aspx";

            Server.Transfer(url);
        }

        /// <summary>
        /// 取得传入的参数
        /// </summary>
        /// <returns></returns>
        private bool GetParma()
        {
            try
            {
                pcsid = Request.Params["pcsid"].ToString();
            }
            catch
            {
               return false;
            }

            return true;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <returns></returns>
        private DataSet GetData()
        {
            //取得传入的参数
            if (GetParma() == false)
            {
                return null;
            }

            DataSet dsResult = new DataSet();

            string error_msg = "";

            //构造SQL语句
            string sql = @"select a.pcsid, b.productcode, c.productname, b.in_qty
                              from purchase a, purchasedetail b, product c
                               where a.pcsid=b.pcsid
                                 and b.productcode=c.productcode
                                 and a.pcsid='{0}'";


            sql = String.Format(sql, pcsid);

            Execute exec = new Execute(Session["connectionString"].ToString());

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                return null;
            }


            //复制报表的数据表结构
            WebInvoicingsys.system.storage.purchasein.purchasein_recv_print_ds report_ds = new purchasein_recv_print_ds();

            DataTable dt = report_ds.dt.Clone();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["pcsid"] = dr["pcsid"].ToString();
                drnew["productcode"] = dr["productcode"].ToString();
                drnew["productname"] = dr["productname"].ToString();
                drnew["qty1"] = Decimal.Parse(dr["in_qty"].ToString());

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            return dsResult;
        }


        private void Bind()
        {
            if (GetParma() == false)
            {
                Return();
            }

            DataSet ds = GetData();

            if (ds == null)
            {
                Return();
            }

            Microsoft.Reporting.WebForms.ReportDataSource rds = new ReportDataSource("dtList", ds.Tables[0]);

            rvPrint.LocalReport.DataSources.Clear();
            rvPrint.LocalReport.DataSources.Add(rds);
            rvPrint.LocalReport.Refresh();

            rvPrint.ShowPrintButton = true;
            rvPrint.LocalReport.DisplayName = "采购入库清单";

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string login = Session["login"].ToString();

                    if (login.ToUpper() != "1")
                    {
                        Response.Redirect("/default.aspx", false);
                        return;
                    }
                }
                catch
                {
                    Response.Redirect("/default.aspx", false);
                    return;
                }

                Bind();
            }
        }

        //返回
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Return();
        }
    }
}